<?php
namespace Home\Controller;
use Think\Controller;
class TrainingController extends BaseController {
    // 崔晓真 培训列表
    public function traininglist(){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $trainings = M('trainings');
        $qrcodenum=$trainings->count();
        $Page = new \Think\Page($qrcodenum,4);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
    每页<b>4</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme',' %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->pageNav=$Page->show();//在页面{pageNav}
        //$this->link=$Page->show();
        $this->qrcodeList=$trainings->limit($Page->firstRow,4)->order('training_id desc')->select();
        $this->display();
    }

    // 崔晓真 删除培训
    public function delete($training_id){
    $trainingsModel=M('trainings');
        $res=$trainingsModel->where("training_id=$training_id")->find();
        $photoPath = realpath("./Public/upload/trainingpic/".$res['training_picture']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($trainingsModel->delete($training_id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }


    public function addtraining(){
        $this->display();

    } 

    // 崔晓真 添加培训
    Public function dotraining(){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
        $trainingModel = M('trainings'); // 实例化User模型
        $trainingModel->training_name =  I('post.training_name'); // 设置用户的创建时间
        $trainingModel->training_time =  I('post.training_time'); // 设置用户的创建时间
        $trainingModel->training_address =  I('post.training_address');
        $trainingModel->training_intro =  I('post.training_intro');
        $trainingModel->training_note =  I('post.training_note');
        $trainingModel->training_price =  I('post.training_price');
        $trainingModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
         //dump($data);
        if ($file['training_picture']['size']==0) {//没有上传轮播图片
            //$this->error('请上传背景图片！');                         
        }else{//有图片
            $info=$this->MyUpload();
            $trainingModel->training_picture = $info['training_picture']['savename']; // 保存上传的照片根据需要自行组装
        }
        $addresult=$trainingModel->add(); // 写入用户数据到数据库
        if (false !== $addresult){
            $this->success('上传培训成功！', '../Training/traininglist');
        }else{ 
            $this->error('数据错误'); 
        }
        
    }

    //崔晓真 上传图片
    public function MyUpload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = 3145728 ;// 设置附件上传大小
        //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =  './';
        $upload->savePath =  'Public/upload/trainingpic/';// 设置附件上传目录

        $upload->autoSub = false;
        $upload->saveName = date("YmdHis").rand(100,999);//定义名字
        // 上传单个文件 
        $info = $upload->upload();                    
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功 获取上传文件信息
                return $info;
            }
    } 

    // 崔晓真 修改培训
    function edittraining($training_id){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $trainings = M('trainings');
        $res = $trainings->where("training_id = $training_id")->find($data);
        $this->list=$res;
        $this->display();
    }

    // 崔晓真 修改培训
    function doUpdate($training_id){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;

        $trainingsmodel=M('trainings');
        if($_FILES['training_picture']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['training_name']=I('post.training_name');
        $data['training_picture'] = $pname;
        $data['training_time']=I('post.training_time');
        $data['training_address']=I('post.training_address');
        $data['training_intro']=I('post.training_intro');
        $data['training_note']=I('post.training_note');
        $data['training_price']=I('post.training_price');
        $res=$trainingsmodel->where("training_id = $training_id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','traininglist');
        } else {
            $this->error('信息修改失败');
        }
    }

    // 崔晓真 培训详情
    public function detail($training_id){
        $admin=M('admins');
        $admins=$admin->find();
        $this->admin=$admins;
        
        //dump($id);
        $trainingsModel=M('trainings');//实例化一个model对象
        $res=$trainingsModel->where("training_id = $training_id")->select();//asc升序 desc降序
        $this->detail=$res;
        //dump($this->detail);
        $this->display();
        }
}